﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation
{
    public class SlotMines: Slot
    {   
        protected float RangedFlightSpeed;
        protected double rangedAttackTimerMillis = 0;
        public SlotMines(WorldProperties worldProperties, SlotKind kind, Tower ownerTower)
            : base(worldProperties, kind, ownerTower,false)
        {
            rangeDisplayColor = Color.Beige;
            RangedFlightSpeed = 500;
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            Sprite = Game.Content.Load<Texture2D>("Sprites/mine_thrower_1");
        }

        public override void Update(GameTime gameTime)
        {
            
            base.Update(gameTime);

            Rotation = ownerTower.Rotation + Orientation;
            if (!building)
            {
                rangedAttackTimerMillis += gameTime.ElapsedGameTime.Milliseconds;


                if (rangedAttackTimerMillis > attackDelayAffected)
                {
                    rangedAttackTimerMillis = 0;
                    RangedAttack(gameTime);
                }
            }
        }
        
        protected  void RangedAttack(GameTime gameTime)
        {

            float slotRotation = orientation + ownerTower.Rotation;

            //get the minimum angle
            float min_angle = ((slotRotation - angle));


            //get the maximum angle
            float max_angle = ((slotRotation + angle));
            
            float randomAngle = (float)((max_angle - min_angle) * worldProperties.Random.NextDouble() + min_angle);
            float randomRange = (float)((rangeAffected - Tower.RADIUS-10) * worldProperties.Random.NextDouble() + Tower.RADIUS+10);
            Vector2 bulletVelocity = new Vector2((float)Math.Cos(randomAngle), (float)Math.Sin(randomAngle));
            Vector2 bulletPosition = ownerTower.Position + Vector2.Normalize(bulletVelocity * RangedFlightSpeed) * (ownerTower.CollisionRadius + 10); 
            new Mine(worldProperties, true, bulletPosition, bulletVelocity, RangedFlightSpeed, new Vector2(10, 10), randomRange, damageAffected);
        }
    }
}

